package com.yun.drools.util;

import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.springframework.stereotype.Component;

import org.drools.runtime.Globals;

import com.yun.entity.User;

@Component
public class MyDrools {
 
	private static KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
	private static Collection<KnowledgePackage> pkgs;
	private static KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
	private static StatefulKnowledgeSession ksession;
	private static List<String> list = new ArrayList<String>();

	static {
		System.setProperty("drools.dateformat", "yyyy-MM-dd");
		kbuilder.add(ResourceFactory.newFileResource("D:/test/person.drl"), ResourceType.DRL);
		pkgs = kbuilder.getKnowledgePackages();
		kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(pkgs);
		ksession = kbase.newStatefulKnowledgeSession();
		ksession.setGlobal("personList", list);
	}

	public void main(final String[] args) {
		
//		ksession.
		
//		ksession.getGlobals();

		// initDrools();
		// initMessageObject();
		// fireRules();

	}

	public void execRule(Object obj) {
		ksession.insert(obj);
	}

	public int fireAllRules() {
		return ksession.fireAllRules();
	}

	public void dispose() {
		ksession.dispose();
	}
	
	public Globals getGlobals(){
		return ksession.getGlobals();
	}
	
	
    
    
//	public static StatefulKnowledgeSession getKsn() {
//		KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
//		builder.add(ResourceFactory.newFileResource("D:/test/book_price.drl"),ResourceType.DRL);
//		// get the compiled packages (which are serializable)
//		Collection<KnowledgePackage> pkg = builder.getKnowledgePackages();
//		KnowledgeBase base = KnowledgeBaseFactory.newKnowledgeBase();
//		// add the packages to a knowledgebase (deploy the knowledge packages).
//		base.addKnowledgePackages(pkg);
//		StatefulKnowledgeSession ksn = base.newStatefulKnowledgeSession();
//		return ksn;
//	}
	
//	public static void initDrools() {
//		kbuilder.add(ResourceFactory.newFileResource("D:/test/gzzqDrl.drl"), ResourceType.DRL);
//		pkgs = kbuilder.getKnowledgePackages();
//		kbase = KnowledgeBaseFactory.newKnowledgeBase();
//		kbase.addKnowledgePackages(pkgs);
//		ksession = kbase.newStatefulKnowledgeSession();
//	}
    
    
    
  
//    private static void initDrools(){
// 
//        // this will parse and compile in one step
//        // read from file
//        kbuilder.add( ResourceFactory.newClassPathResource( "/hellodrools/testrules.drl",MyDrools.class),ResourceType.DRL );
//        kbuilder.add( ResourceFactory.newFileResource("D:/book_price.drl"),ResourceType.DRL);
// 
//        // read second rule from String
//        String myRule = "import hellodrools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
//        Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
//        kbuilder.add(myResource, ResourceType.DRL);
// 
//        // Check the builder for errors
//        if ( kbuilder.hasErrors() ) {
//            System.out.println( kbuilder.getErrors().toString() );
//            throw new RuntimeException( "Unable to compile drl\"." );
//        }
// 
//        // get the compiled packages (which are serializable)
//        pkgs = kbuilder.getKnowledgePackages();
// 
//        // add the packages to a knowledgebase (deploy the knowledge packages).
//        kbase.addKnowledgePackages( pkgs );
// 
//        ksession = kbase.newStatefulKnowledgeSession(); 
//        }
 
//    private static void fireRules(){
//        ksession.fireAllRules();
//        ksession.dispose();
//    }
 
	
}
